package com.stn.zjx.givingtreasure;

import android.animation.Animator;
import android.os.Bundle;
import android.widget.RelativeLayout;


import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.constants.StnConstants;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.vondear.rxtools.RxActivityTool;
import com.vondear.rxtools.RxSPTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by 朱俊鑫 on 2018-3

 */

public class SplashActivity extends BaseActivity {
    @BindView(R.id.bg_rl)
    RelativeLayout bgRl;
    public boolean isLoading = true;
    public int seconds = 2;
    private Disposable disposable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(RxSPTool.getBoolean(this, StnConstants.STOP_START_ANIM)){
            RxActivityTool.skipActivityAndFinish(SplashActivity.this,MainActivity.class);
            return ;
        }
        setContentView(R.layout.activity_main_splash);
        ButterKnife.bind(this);

        initComponent();

    }
    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }



    @Override
    public void initComponent() {
        disposable=Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                while (isLoading && seconds > 0) {
                    try {
                        Thread.sleep(1500);
                        emitter.onNext(seconds);
                        seconds--;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                emitter.onComplete();
            }
              }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        isLoading=true;
                        RxActivityTool.skipActivityAndFinish(SplashActivity.this,MainActivity.class);
                    }
                })
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        isLoading = false;
                        if(integer==2){
                            StatusBarUtil.setImmersiveStatusBar(SplashActivity.this,false);
                            initStatusBarColor(R.color.default_black);
                        }

                    }
                });


    }


}
